package com.osgix.authorize.web.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.osgix.authorize.model.UserInfo;
import com.osgix.authorize.service.UserInfoManager;

@Controller
public class UserController {

	@Resource
	UserInfoManager userInfoManager;

	@RequestMapping(value = "/customer/login", method = RequestMethod.GET)
	public ModelAndView login() {
		ModelAndView mav = new ModelAndView("customer/login");
		System.out.println(">>>>logining");
		return mav;
	}

	// @PathParam是对资源路么参数的解析，它会自动根据提交的数据格式，解析参数值。
	@RequestMapping(value = "/customer/{loginAccount}", method = RequestMethod.GET)
	public ModelAndView showUser(@PathVariable String loginAccount, Model model) {
		ModelAndView mav = new ModelAndView("customer/userInfo");
		UserInfo user = userInfoManager.getByLoginAccount(loginAccount);
		model.addAttribute("user", user);
		return mav;
	}
	
	@RequestMapping(value = "/save/{userInfoId}", method = RequestMethod.GET)
	public ModelAndView saveUser(@PathVariable Long userInfoId, Model model) throws Exception {
		ModelAndView mav = new ModelAndView("customer/userInfo");
		UserInfo user = new UserInfo();
		user.setUserId(userInfoId);
		user.setLoginAccount("hahaha");
		user.setFirstName("jjj");
		user.setUserName("nameuuu");
		user.setLoginEmail("eeemmmaaa");
		userInfoManager.saveUserInfo(user);
		model.addAttribute("user", user);
		return mav;
	}
}